import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import router from '@/router/index'

// 创建axios实例
const service = axios.create({
  // baseURL: process.env.BASE_API, // api的base_url
  baseURL: window.localStorage.url, // api的base_url
  timeout: 15000                  // 请求超时时间
})

//request拦截器
service.interceptors.request.use(config => {
  // if (store.getters.token) {
  //   config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  // }
  //设置请求头携带cookie、跨域session
  config.withCredentials = true;
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

//统一拦截response
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    const res = response.data
    if (res.status == 'LOSE') {
      MessageBox.alert('你已被登出，可以取消继续留在该页面，或者重新登录', '确定登出', {
        confirmButtonText: '确定',
        type: 'warning'
      }).then(() => {
        localStorage.clear()
        router.replace({
          path: '/'
        })
        return
      }).catch(() => {
        localStorage.clear()
        router.replace({path: '/'})
      })
    }else if (res.status == 'FAILED') {
        Message({
          message: res.data,
          type: 'error',
          duration: 3 * 1000
        })
      return Promise.reject('error')
    }
    else {
      return response.data
    }
  },
  error => {
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = '请求错误';
          router.replace({path: '/'})
          break
        case 401:
          error.message = '未授权，请登录';
          router.replace({path: '/'})
          break
        case 403:
          error.message = '拒绝访问'
          router.replace({path: '/'})
          break
        case 404:
          // error.message = (process.env.NODE_ENV === 'production' ? `请求地址出错` : `请求地址出错: ${error.response.config.url}`)
          router.replace({path: '/'})
          break
        case 408:
          error.message = '请求超时'
          router.replace({path: '/'})
          break
        case 500:
          error.message = '请重新登录'
          router.replace({path: '/'})
          break
        case 501:
          error.message = '服务未实现'
          router.replace({path: '/'})
          break
        case 502:
          error.message = '网关错误'
          router.replace({path: '/'})
          break
        case 503:
          error.message = '服务不可用'
          router.replace({path: '/'})
          break
        case 504:
          error.message = '网关超时'
          router.replace({path: '/'})
          break
        case 505:
          error.message = 'HTTP版本不受支持'
          router.replace({path: '/'})
          break
        default:
      }
      Message({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
      })
    }
    return Promise.reject(error)
  }
)
export default service

// router.beforeEach((to, from, next) => {
//   if (to.meta.login) {
//     if (window.localStorage.token != undefined) {  // 通过store获取当前的token是否存在
//       next();
//     } else {
//       MessageBox.alert('未登录，请先登录').then(()=>{ //promise
//         next({
//           path: '/',
//           query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
//         })
//       })
//     }
//   }
//   else {
//     next();
//   }
// })
